import * as echarts from "../../../components/ec-canvas/echarts"
let Chart = null;
Component({ 
    data: {
        weatherData: [{
                name: '周一',
                date: '01-04',
                status: '晴'
            },
            {
                name: '周二',
                date: '01-05',
                status: '雨'
            },
            {
                name: '周三',
                date: '01-06',
                status: '多云'
            },
            {
                name: '周四',
                date: '01-07',
                status: '晴'
            },
            {
                name: '周五',
                date: '01-08',
                status: '晴'
            }
        ],
        echartsData: {
            color: ["#F7DD8A","#2EB356"],
            grid: {
                left: '0',
                right: '0'
            },
            xAxis: {
                type: 'category',
                boundaryGap: ['10%', '10%'],
                show: false
            },
            yAxis: {
                min: 'dataMin',
                max: function (value) {
                    return value.max + 10;
                },
                show: false
            },
            series: [{
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            textStyle: {
                                color: '#333'
                            },
                            formatter: function (params) {
                                return params.value + '°'
                            }
                        }
                    }
                },
                type: 'line',
                symbolSize: '4',
                data: [11, 20, -8, 40, 14]
            },
			{
			    itemStyle: {
			        normal: {
			            label: {
			                show: true,
			                textStyle: {
			                    color: '#333'
			                },
			                formatter: function (params) {
			                    return params.value + '°'
			                }
			            }
			        }
			    },
			    type: 'line',
			    symbolSize: '4',
			    data: [11, 20, -8, 40, 14]
			}]
        }
    },
    lifetimes: {
        // ready() {
        //     this.echartsComponnet = this.selectComponent('#mychart-dom-bar')
        //     this.init_echarts();
        // },

    },
    methods: {
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad(options) {
            // console.log("天气预报");
            const eventChannel = this.getOpenerEventChannel()
            let that = this;
            eventChannel.on('weather', function (data) {
                console.log("7天天气预报", data);
                let weatherData=[];
                let airTempreture=[];
				let airLowTempreture=[];
                for(let i=0;i<5;i++){
                    let val=data[i];
                    let arr=val.daytime.split("");
					
                    weatherData.push({name:arr[4]+arr[5]+"-"+arr[6]+arr[7],status:val.day_weather})
                    airTempreture.push(val.day_high_temperature)
					airLowTempreture.push(val.night_low_temperature)
                }
            
                that.data.echartsData.series[0].data=airTempreture
				that.data.echartsData.series[1].data=airLowTempreture
                that.setData({
                    weatherData,
                    echartsData:that.data.echartsData
                });
                that.echartsComponnet = that.selectComponent('#mychart-dom-bar')
                that.init_echarts();
                // console.log("weatherData",that.data.weatherData);
                console.log("echartsData",that.data.echartsData);
            })
        },
        init_echarts() {
            this.echartsComponnet.init((canvas, width, height, dpr) => {
                Chart = echarts.init(canvas, null, {
                    width: width,
                    height: height,
                    devicePixelRatio: dpr
                });
                Chart.setOption(this.getOption());
                return Chart;
            });
        },
        getOption() {
            var option = this.data.echartsData;
            // console.log("option",option);
            return option;
        }
    },
    onUnload: function () {
        Chart = null
    }
})